import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:xindong/common/xindong_sezhi.dart';
import 'package:xindong/common/xindong_define.dart';
import 'package:xindong/manager/xindong_chat_singleton.dart';
import 'package:xindong/res/assets_res.dart';
import 'package:xindong/routes/tab_chats/controller/xindong_liaotian_vm.dart';
import 'package:xindong/utils/xindong_image_util.dart';
import 'package:xindong/widgets/common/xindong_app_bar.dart';
import 'package:xindong/widgets/common/xindong_app_bar_icon_button.dart';
import 'package:xindong/widgets/xindong_avatar.dart';
import 'package:xindong/widgets/xindong_count_red_dots.dart';

Widget conversationAppBar({required String title, required Function() onTap}) {
  return GetBuilder<XinDongChatSingleton>(builder: (controller) {
    if (controller.detailClass == null) {
      return XinDongAppBar(
        "",
        leadingWidth: 100, // 保证能显示未读数量
        leading: _backWidget(),
        actions: [_moreWidget(onTap)],
      );
    }
    return XinDongAppBar(
      "",
      leading: const SizedBox(),
      leadingWidth: 0,
      titleView: Stack(
        alignment: Alignment.center,
        children: [
          Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              const Expanded(child: SizedBox()),
              XinDongAvatar(url: controller.detailClass!.another.getAvatar, size: 40),
              Container(
                height: kAppBarHeight,
                padding: const EdgeInsets.only(left: 10, right: 10),
                child: Stack(
                  alignment: Alignment.topCenter,
                  children: [
                    ImageUtil.asset(AssetsRes.CHAT_HEART, width: 50),
                    Positioned(
                      bottom: 5,
                      child: Text(
                        "${controller.detailClass!.data.intimate}°C",
                        style: const TextStyle(fontSize: 10, color: DZColors.contentColor),
                      ),
                    ),
                  ],
                ),
              ),
              XinDongAvatar(url: controller.detailClass!.self.getAvatar, size: 40),
              const Expanded(child: SizedBox()),
            ],
          ),
          Positioned(left: 0, child: _backWidget()),
          Positioned(right: 0, child: _moreWidget(onTap)),
        ],
      ),
    );
  });
}

Widget _backWidget() {
  return GestureDetector(
    onTap: () => Get.back(),
    behavior: HitTestBehavior.opaque,
    child: Stack(
      alignment: Alignment.centerLeft,
      clipBehavior: Clip.none,
      children: [
        const XinDongAppBarIconButton(),
        Positioned(
          left: kPaddingValue + 26,
          child: Obx(
            () => XinDongUnreadCountWidget(
              count: XinDongLiaoTianVM.to.unreadCount.value,
              maxCount: 99,
            ),
          ),
        ),
      ],
    ),
  );
}

Widget _moreWidget(Function() onTap) {
  return XinDongAppBarIconButton(icon: Icons.more_horiz_rounded, onTap: onTap);
}
